package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.LoginAnnotation;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyDataRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserInfoController {

    // 获取配置文件中发送短信需要的url以及appkey
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String smsAppKey;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyDataRedisService strategyDataRedisService;

    /**
     * 通过用户id获取用户信息
     * @param id
     * @return UserInfo
     */
    @GetMapping("/get")
    public Object list(String id){
        return userInfoService.get(id);
    }


    /**
     * 通过手机号获取用户信息
     * @param phone
     * @return boolean true表示可以注册,false表示已经注册
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        UserInfo userInfo = userInfoService.checkPhone(phone);
        // 如果 userInfo 为空,则该手机号可以注册 返回true,反之返回false
        return userInfo == null;
    }

    /**
     * 发送短信验证码以及缓存验证码信息到redis
     * @param phone
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,smsUrl,smsAppKey);
        return JsonResult.success();
    }

    /**
     * 用户注册
     * @param phone 手机号
     * @param nickname 昵称
     * @param password 密码
     * @param rpassword 确认密码
     * @param verifyCode 验证码
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){

        userInfoService.save(phone,nickname,password,rpassword,verifyCode);

        return JsonResult.success();
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username,String password){

        // 登录成功之后,返回用户对象,需要存储到 Redis
        UserInfo user = userInfoService.userLogin(username,password);


        // 将用户信息存储至 Redis
        String key = userInfoRedisService.setUserInfo(user);

        // 创建map 用户存储token和user对象,返回给浏览器
        HashMap map = new HashMap();
        map.put("token",key);
        map.put("user",user);

        return JsonResult.success(map);
    }

    @LoginAnnotation
    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest req){
        // 获取请求头 传递的token信息
        String token = req.getHeader("token");

        // 通过token 获取用户信息
        UserInfo user =  userInfoRedisService.getUserInfo(token);

        // 如果token已经过期或者不存在
        if(user == null){
            // 提醒用户重新登录
            throw new LogicException("亲,请重新登录");
        }else{
            // 否则 返回用户信息
            return JsonResult.success(user);
        }

    }

    @LoginAnnotation
    @GetMapping("/strategies/favor")
    public Object favor(@UserParam UserInfo user){
        List<String> sids = strategyDataRedisService.getFavorList(user.getId());
        return JsonResult.success(sids);
    }



}
